<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Settings {

	private $CI;
	
	private $items = array();
	
	function __construct() {
		$this->CI =& get_instance();
		$this->CI->load->model('Setting_Model');
		$this->CI->load->config("settings");
		
		$this->_init();
	}
	
	function _init(){
		//Cargamos la configuracion general
		$settings_autoload = $this->CI->Setting_Model->get();
		foreach($settings_autoload as $setting){
			$this->items[$setting->setting_key] = $setting->setting_value;
		}
		
	}
	
	function getGroups(){
		return $this->items['groups'];
	}
	
	function item($slug = '') {
		
		$value = NULL;
		
		// Check for previously called settings and return it if found
		if(in_array($slug, array_keys($this->items)))
        {
        	return $this->items[$slug];
        }
		
		// Check the database for this setting
		if ($setting = $this->CI->Setting_Model->get($slug))
		{
        	$value = $setting->setting_value;
        } 	
        
        // Not in the database? Try a config value instead
        else if($value = $this->CI->config->item($slug))
        {
            $value = $this->CI->config->item($slug);
        }
        
        // Save the value in the items array and return it
        return $this->items[$slug] = $value;
    }
    
    function set_item($slug, $value, $autoload = NULL) {
    	$this->items[$slug] = $value;
    	return $this->CI->Setting_Model->update($slug, $value, $autoload);
    }
    
    function set_settings($params, $autoload = NULL){
    	foreach($params as $key=>$value){
    		if($key != "submit"){
    			
    			Console::log("Editando Opcion '$key' con valor '$value' y autoload: '$autoload'");
    			if($autoload != NULL){
    				Console::log("Autoload distinto de NULL y con valor: '$autoload'");
    			}
    			
    			$this->set_item($key, $value, $autoload);	
    		}
    	}
    }
    
}

?>